<?php

namespace App\Kuafu\Shop\Controller\B;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Export\Model\ExportDownloadModel;
use App\Kuafu\Export\Service\ExportDownloadService;
use App\Kuafu\Order\Enum\OrderMerchantEnum;
use App\Kuafu\Shop\Service\ShopRecordService;
use Illuminate\Http\Request;


class ShopRecordController extends BaseController
{
    // 门店备案
    public function list(Request $request)
    {
        $pageSize         = (int)$request->get('pageSize', 10);
        $pageNum          = (int)$request->get('pageNum', 1);
        $dimCode          = $request->get('dimCode', '');
        $shopIds          = $request->get('shopIds', '');
        $status           = $request->get('status','');
        $areaCode         = $request->get('areaCode', 0);
        $inShopStaffCount = $request->get('inShopStaffCount', '');
        $recordStatus     = $request->get('recordStatus', '');
        $councilorId      = $request->get('councilorId', 0);
        $sortField        = $request->get('sortField', '');
        $sortOrder        = $request->get('sortOrder', '');
        $export           = (bool)$request->get('export', false);
        $activityDays     = (int)$request->get('activityDays', 0);  // 0 无限制

        if ($shopIds) {
            $shopIds = explode(',', $shopIds);
        }
        $orderBy = [];
        if ($sortField && $sortOrder) {
            $orderMap = [
                'ascend'  => 'asc',
                'descend' => 'desc'
            ];
            $order    = $orderMap[$sortOrder] ?? '';
            if (empty($order)) {
                return $this->fail(BaseCodeEnum::ERR_400000, '排序字段异常');
            }
            $orderBy = [$sortField => $order];
        }

        if ($export) {
            $params = [
                'pageSize'         => $pageSize,
                'pageNum'          => $pageNum,
                'dimCode'          => $dimCode,
                'shopIds'          => $shopIds,
                'status'           => $status,
                'areaCode'         => $areaCode,
                'inShopStaffCount' => $inShopStaffCount,
                'recordStatus'     => $recordStatus,
                'councilorId'      => $councilorId,
                'sortField'        => $sortField,
                'activityDays'     => $activityDays,
            ];
            ExportDownloadService::export(ExportDownloadModel::CLIENT_TYPE_WEB,
                ExportDownloadModel::FILE_TYPE_WEB_SHOP_RECORD_LIST, 0,
                OrderMerchantEnum::SHOW_TIME_TAB_DEFINE, strtotime('-1 day'), time(), [], $params
            );
            return $this->success();
        }


        return $this->success(ShopRecordService::getList(
            $pageSize, $pageNum, $shopIds, $dimCode, $areaCode, $status, $inShopStaffCount, $recordStatus, $councilorId,
            $orderBy, false, $activityDays
        ));
    }

}
